package com.example.wechat;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import java.util.List;

@Dao
public interface ContactDao {
    @Insert
    void insert(Contact contact);

    @Update
    void update(Object contact);

    @Delete
    void delete(Contact contact);

    @Query("SELECT * FROM contacts ORDER BY firstLetter ASC")
    LiveData<List<Contact>> getAllContacts();

    @Query("SELECT * FROM contacts WHERE group = :groupName ORDER BY firstLetter ASC")
    LiveData<List<Contact>> getContactsByGroup(String groupName);

    @Query("SELECT * FROM contacts WHERE id = :contactId")
    Contact getContactById(String contactId);

    @Query("SELECT DISTINCT group FROM contacts WHERE group IS NOT NULL ORDER BY group ASC")
    LiveData<List<String>> getAllGroups();

    @Query("SELECT * FROM contacts WHERE name LIKE '%' || :query || '%' OR remark LIKE '%' || :query || '%'")
    LiveData<List<Contact>> searchContacts(String query);

    @Query("UPDATE contacts SET avatarUrl = :avatarUrl WHERE id = :contactId")
    void updateAvatar(String contactId, String avatarUrl);

    @Query("UPDATE contacts SET remark = :remark WHERE id = :contactId")
    void updateRemark(String contactId, String remark);

    @Query("UPDATE contacts SET group = :groupName WHERE id = :contactId")
    void updateGroup(String contactId, String groupName);
} 